/**@FILE:OrderAction.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-2-1 上午11:40:07*/
package com.xf.toy.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xf.pub.exception.ServiceException;
import com.xf.pub.logger.LoggerUtil;
import com.xf.toy.bean.Toyorder;
import com.xf.toy.dao.page.Pagination;
import com.xf.toy.service.OrderService;
import com.xf.toy.service.constant.ToyConst;
import com.xf.toy.web.common.BaseAction;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:OrderAction
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-2-1 上午11:40:07
 *******************************************/
@Controller
@RequestMapping(value = "/toy/order")
public class OrderAction extends BaseAction {
	@Resource(name = "orderService")
	private OrderService orderService;

	private Long userid;

	@RequestMapping(value = "/create")
	public void creteOrder(@RequestParam Long wishid, HttpServletRequest req,
			HttpServletResponse resp) {
		try {
			userid = super.getUserId(req);
			operLogger.info(LoggerUtil.logstr("method", "toy/order/create",
					"userid", userid + ""));
			orderService.createOrderByWishId(wishid);
			Toyorder order=orderService.selectOrderByWishId(wishid);
			response(resp, order);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public void detail(@RequestParam Long orderid, HttpServletRequest req,
			HttpServletResponse resp) {
		try {
			userid = super.getUserId(req);
			operLogger.info(LoggerUtil.logstr("method", "toy/order/detail",
					"userid", userid + ""));
			Toyorder order = orderService.selectOrderDetail(orderid);
			response(resp, order);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	public void orderlist(@RequestParam(required = false) Long orderid,
			@RequestParam(required = false) String toycode,
			@RequestParam(required = false) Long userid,
			@RequestParam(required = false) Integer pageNo,
			@RequestParam(required = false) Integer pageSize,
			@RequestParam(required = false) String status,
			HttpServletRequest req, HttpServletResponse resp) {
		try {
			Pagination page = new Pagination();
			if (pageNo != null) {
				page.setPageSize(pageSize);
				page.setCurrentPage(pageNo);
			}
		  Long	myuserid = super.getUserId(req);
			operLogger.info(LoggerUtil.logstr("method", "toy/order/orderlist",
					"userid", myuserid + ""));
			List<String> statusset = new ArrayList<String>();
			if (status == null) {
				statusset.add(ToyConst.TOY_ORDER_STATUS_DELIVER);
				statusset.add(ToyConst.TOY_ORDER_STATUS_WAITRETURN);
			} else {
				String[] orderArray = status.split(",");
				for (int i = 0; i < orderArray.length; i++) {
					statusset.add(orderArray[i]);
				}
			}
			List<Toyorder> orders = orderService.selectOrderList(userid, toycode,orderid,
					statusset, page);
			response(resp, orders,page);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

}
